<?php 
include_once('Database.php');
include_once('model/Graph.php');

 

$path = $_SERVER['PATH_INFO']; 

if ($path != null) {
    $path_params = explode ("/", $path);
}


if ($_SERVER['REQUEST_METHOD'] == 'GET') {
	
	if ($path_params[1] != null && $path_params[2] != null && $path_params[3] != null) {
			get_attributes($path_params[1],$path_params[2],$path_params[3]);
        }	
		
	} 


function get_attributes($vehicle_id, $graphElement1, $graphElement2) {
        $db = new DBManager();
        $mysqli = $db->db_connect();
		
	    $query = "SELECT v.vehicleId, v.currentTime, v.$graphElement1, v.$graphElement2 FROM VehicleInfo as v WHERE v.vehicleId = ?";
	    $stmt=$mysqli->prepare($query);
	    $stmt->bind_param('i',$vehicle_id);
        $stmt->execute();	
        $result = $stmt->get_result();	
		
	    print_result($result,$graphElement1,$graphElement2);
}


function print_result($result, $graphElement1, $graphElement2) {

	header("Content-Type: text/json");
	$graphInstance = new Graph($graphElement1, $graphElement2);
	$temp_arr = array();
	
	while ($row =$result->fetch_assoc()) {
		array_push($temp_arr, array($row["$graphElement1"],$row["$graphElement2"]));
		$graphInstance->setData($temp_arr);
	}

	echo json_encode($graphInstance->jsonSerialize());
}
 
		    

?>